package tk.mybatis.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import tk.mybatis.springboot.mapper.*;
import tk.mybatis.springboot.model.Region;
import tk.mybatis.springboot.model.Sites;
import tk.mybatis.springboot.model.Users;
import tk.mybatis.springboot.model.Waybill;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by  on 2017/4/17.
 */
@Controller
public class CoreController {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private SitesMapper sitesMapper;
    @Autowired
    private WaybillMapper waybillMapper;
    @Autowired
    private WaybillStatusMapper waybillStatusMapper;

    @RequestMapping("")
    public String index(Model model,@ModelAttribute("key") String key){
        if(!StringUtils.isEmpty(key)){
            Waybill waybill = waybillMapper.selectByPrimaryKey(key);
            if(null == waybill){
                model.addAttribute("msg","订单不存在");
            }
            else{
                model.addAttribute("list",waybillStatusMapper.list(key));
            }
        }
        return "index";
    }
    @RequestMapping("/admin")
    public String admin(Model model,HttpSession session){
        model.addAttribute("me",session.getAttribute("me"));
        return "admin";
    }
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(){
        return "login";
    }
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(String username, String password, HttpSession session){
        Users users = userMapper.selectOne(new Users() {
            {
                setUsername(username);
                setPassword(password);
            }
        });
        if(null == users){
            throw new RuntimeException("用户名或者密码错误");
        }
        session.setAttribute("me",users);
        if(users.getType().equals("E")) {
            return "redirect:/waybill";
        }
        return "redirect:/users";
    }

    @ExceptionHandler
    public String error(Exception e,Model model){
        model.addAttribute("msg",e.getMessage());
        return "500";
    }

    @RequestMapping(value = "/sites",method = RequestMethod.GET)
    public String sites(Model model){
        List<Sites> sites = sitesMapper.selectAll();
        for(Sites site:sites){
            Region region = regionMapper.selectByPrimaryKey(site.getRegionId());
            if(null != region) {
                site.setRegionName(region.getName());
            }
        }
        model.addAttribute("list",sites);
        model.addAttribute("regions",regionMapper.selectAll());
        return "sites";
    }
    @RequestMapping(value = "/region",method = RequestMethod.GET)
    public String region(Model model){
        model.addAttribute("list",regionMapper.selectAll());
        return "region";
    }
    @RequestMapping(value = "/waybill",method = RequestMethod.GET)
    public String waybill(Model model){
        model.addAttribute("list",waybillMapper.selectAll());
        return "waybill";
    }
    @RequestMapping(value = "/users",method = RequestMethod.GET)
    public String users(Model model){
        model.addAttribute("list",userMapper.selectAll());
        return "users";
    }
}
